﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Turtle8
{
    public partial class TahasukaDuringPostCard : UserControl
    {
        TahasukaCard tCard;
        PictureBox picBox = new System.Windows.Forms.PictureBox();
        Dictionary<string, PostPanel> postDict = new Dictionary<string, PostPanel>();
        public string SelectedPostName { set; get; }
        PostPanel currentPost;

        public TahasukaDuringPostCard(TahasukaCard tCard)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.tCard = tCard;

            //set picBox properties
            picBox.SizeMode = PictureBoxSizeMode.StretchImage;
            String currentDir = System.IO.Directory.GetCurrentDirectory();
            picBox.Image = Image.FromFile(currentDir + @"\images\post.jpg");
            picBox.Dock = DockStyle.Fill;
            changeMainPanel(picBox);

            //retrieves all the posts already created for this Tahasuka
            SqlCommand selectCmd = SqlManager.SqlCommandFactory(@"SELECT postName FROM [TahasukaPosts] WHERE startDate=@startDate");
            selectCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = tCard.StartDate;
            SqlDataReader reader = selectCmd.ExecuteReader();

            //initializing all already exist posts.
           
            //init the keys in the dictionary
            while (reader.Read())
                postDict.Add((string)reader[0], null);
            reader.Close();

           //create a PostPanel instance for each key
            List<string> keys_list = postDict.Keys.ToList<string>();
            for (int i = 0; i < postDict.Count ; i++)
                postDict[(string)keys_list[i]] = new PostPanel(keys_list[i], tCard.StartDate);   
            
               

            reader.Close();
            //case the list has items, i choose the first post to be the default post.
            if (postDict.Count != 0)
            {
                currentPost = postDict.First().Value;
                changeMainPanel(currentPost);
            }
            //else i keep the picBox as the control in SplitContainer.panel2

        }


        private void changeMainPanel(Control ctrl)
        {
            if (!splitContainer.Panel2.Contains(ctrl))
            {
                this.splitContainer.Panel2.Controls.Clear();
                this.splitContainer.Panel2.Controls.Add(ctrl);
                this.splitContainer.Panel2.Show();
            }
        }

        private void buttonOpenPostCard_Click(object sender, EventArgs e)
        {
            ExistsPostCardView existPostsView = new ExistsPostCardView(this, tCard.StartDate);

            if (existPostsView.DialogResult == DialogResult.Yes)
            {
                existPostsView.ShowDialog();
                if (SelectedPostName != null)
                {
                    currentPost = postDict[SelectedPostName];
                    changeMainPanel(currentPost);
                }
            }
            else
            {
                existPostsView.Dispose();
                MessageBox.Show("אין כרגע כרטיסי עמדה קיימים. על מנת ליצור כרטיס עמדה נא לחץ על הלחצן: צור כרטיס חדש");
            }

        }

        private void buttonAddimage_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
                result.ToString();
        }

        private void buttonCreateNewPostCard_Click(object sender, EventArgs e)
        {
            if (currentPost == null)
                currentPost = new PostPanel(tCard.StartDate);
            else
            {
                DialogResult result = MessageBox.Show("?האם ברצונך לשמור את הכרטיס הנוכחי", "שינויים בכרטיס הנוכחי טרם נשמרו", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    //currentPost.saveCurrentPost();
                    currentPost = new PostPanel(tCard.StartDate);
                }
                else if (result == DialogResult.No)
                    currentPost = new PostPanel(tCard.StartDate);
            }
            changeMainPanel(currentPost);
        }

        private void buttonDeleteCard_Click(object sender, EventArgs e)
        {
            postDict.Remove(currentPost.PostName);
            //currentPost.delete();
          //  changeMainPanel(postDict.ElementAt(i).Value);
         //   contextMenuStrip.
        }

}
}

